/*
 * Copyright (c) 2008 VMware, Inc.
 * Copyright (c) 2009 John Pritchard, WTKX Project Group
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package wtkx;

import wtkx.util.ListenerList;
import wtkx.wtk.MenuButtonListener;
import wtkx.wtk.content.ButtonDataDecorator;

/**
 * Component that allows a user to select one of several menu options. The
 * options are hidden until the user pushes the button.
 * <p>
 * The repeatable flag is used to trigger "split button" behavior.
 * When true, the button reflects the selected value and allows a user to
 * repeatedly press the left half of the button, firing additional menu
 * selection events for the selected item. Pressing the right half of the
 * button continues to fire button press events and display the menu.
 *
 * @author gbrown
 */
public class MenuButton 
    extends Button 
{

    private static final Button.DataDecorator DEFAULT_DATA_RENDERER = new ButtonDataDecorator();

    public class MenuButtonListenerList
        extends ListenerList<MenuButtonListener>
        implements MenuButtonListener 
    {
        MenuButtonListenerList(){
            super();
        }

        public void menuChanged(MenuButton menuButton, Menu previousMenu) {
            for (MenuButtonListener listener : this) {
                listener.menuChanged(menuButton, previousMenu);
            }
        }

        public void repeatableChanged(MenuButton menuButton) {
            for (MenuButtonListener listener : this) {
                listener.repeatableChanged(menuButton);
            }
        }
    }

    private Menu menu = null;
    private boolean repeatable = false;

    private MenuButtonListenerList menuButtonListeners = new MenuButtonListenerList();


    public MenuButton() {
        super();
        this.setDataDecorator(DEFAULT_DATA_RENDERER);
        this.installSkin(MenuButton.class);
    }


    @Override
    public void setToggleButton(boolean toggleButton) {
        throw new UnsupportedOperationException("Menu buttons cannot be toggle buttons.");
    }

    public Menu getMenu() {
        return menu;
    }

    public void setMenu(Menu menu) {
        if (menu != null
            && menu.getItem() != null) {
            throw new IllegalArgumentException("menu already belongs to an item.");
        }

        Menu previousMenu = this.menu;

        if (previousMenu != menu) {
            this.menu = menu;
            menuButtonListeners.menuChanged(this, previousMenu);
        }
    }

    public boolean isRepeatable() {
        return repeatable;
    }

    public void setRepeatable(boolean repeatable) {
        if (this.repeatable != repeatable) {
            this.repeatable = repeatable;
            menuButtonListeners.repeatableChanged(this);
        }
    }

    public ListenerList<MenuButtonListener> getMenuButtonListeners() {
        return menuButtonListeners;
    }
}
